import axios from 'axios';
import { downloadFileTemplate } from '../utils/files';
import type { IQuery, IResponse, TableResponse } from '@/api/config/types';
import type { Field } from './field';
export interface TagStatistics {
  count: number;
  resolutionId: string;
}
export interface Goods {
  goodsId: string;
  goodsName: string;
  barCode: any;
  salesUnit: string;
  retailPrice: undefined;
  memberPrice: undefined;
  salePrice: undefined;
  qrcodeUrl: string;
  refreshTimes: string;
  goodsPhoto: string;
  deptId: string;
  createTime: string;
  expands: any[]; // 或具体定义 expends 的结构
  department: string;
  category: string;
  model: string;
  goodsVideo: string;
  tagCount: TagStatistics[];
  shelfLife: string;
  specif: string;
  sku: string;
  stock: number | undefined;
  itemNo: string;
  promotionBegin: string;
  promotionEnd: string;
  productionDate: string;
  warehouse: string;
  freightSpace: string;
  share: '0' | '1';
  grade: string;
  mode: string;
  origin: string;
  disable: boolean;
}

export interface GoodsRecord {
  recordId: string;
  goodsName: string;
  platform: string;
  rawData: string;
  postData: string;
  deptId: string;
  username: string;
}

const prefix = '/goods/goods';

export const listGoods = (query?: IQuery) =>
  axios.get<TableResponse<Goods>>(`${prefix}/list`, { params: query });

export const exportGoods = (query?: IQuery) => axios.get(`${prefix}/export`, { params: query });

export const delGoods = (goodsId: Array<string>) => axios.delete(`${prefix}/${goodsId.join(',')}`);

export const getGoods = (goodsId: string | number) =>
  axios.get<IResponse<Goods>>(`${prefix}/${goodsId}`);

export const updateGoods = (data: Goods) => axios.put<IResponse>(prefix, data);

export const addGoods = (data: Goods) => axios.post(prefix, data);

export const findGoodsExpandFields = (loadAll = true) =>
  axios.get<IResponse<Field[]>>(`${prefix}/findGoodsExpandFields`, { params: { loadAll } });

export const goodsTemplate = () => axios.get(`${prefix}/downImportTemplate`);

export const downloadFile = () => downloadFileTemplate(`${prefix}/export`);

export const manifestFile = () => downloadFileTemplate(`${prefix}/manifest?scope=0`);

export const setGoodsIsPrivate = (isPrivate: 1 | 0) =>
  axios.get(`${prefix}/touchGenre/${isPrivate}`);

export const setGoodsIsPrivateBatch = (isPrivate: 1 | 0, goodsIds: (string | number)[]) =>
  axios.post<IResponse>(`${prefix}/share/${isPrivate}`, goodsIds);

export const goodsAutoMatch = () => axios.get(`${prefix}/autoMatch`);

export const removeUrl = (id: string | number) => axios.get(`${prefix}/removeUrl/${id}`);

export const goodsRecordList = (query: IQuery) =>
  axios.get(`/goods/record/list`, { params: query });
